home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Allocation of memory
- Date: 4 Feb 1996 07:38:45 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4f2ju5INNlho@keats.ugrad.cs.ubc.ca>
- References: <4evre9$8r4@news.rz.uni-passau.de>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
- Keywords: realloc
-
- In article <4evre9$8r4@news.rz.uni-passau.de>,
- Klaus Berndl <berndl@piligrim.uni-passau.de> wrote:
- >today i have a not-RPC-problem, but perhaps you can solve it:
- >
- >Please look at the foolwing short piece of code:
-
- Let's not and say we did!
-
- >Now my questions:
- >
- >1) How is it possible, that my machine (Sparc classic) allocates 100MB
- > Storage? I`m sure i have not so much main memory installed. Does it
- > use swap-file??
-
- It's called "virtual memory". Here is one hypothesis on how it is done: Your
- malloc() calls the brk() system call, which tells the operating system that you
- have 100MB more worth of pages added to your data segment. These are marked
- "not-present". It is when you access these these pages that the OS will
- actually try to assign to them a piece of virtual memory. It's sort of an
- "allocate on write".
-
- So you won't see any thrashing until you start using the memory.
-
- >2) With ALLOCSIZE set to 100000000 the program works correct. But if i set
- > ALLOCSIZE to 300000000, i reach the following output:
- >
- > Klaus
- >
- > t-realloc failed
- >
- > test1
- > Bus Error (core dumped)
-
- It could be that the OS will refuse to honor a brk() call that would result in
- more pages being allocated than the free pages in the virtual memory pool.
-
- > What is 'realloc' doing exactly? Normally if realloc can`t allocate the
- > requested memory it returns NULL and doesn`t allocate memory. Why the
- > second realloc-call (x = ...) fails? There must be 1000000 Byte memory
- > because of the first try with ALLOCSIZE 100000000 works!
-
- Realloc uses the same routines as malloc() to allocate heap storage. When it
- can't allocate, it will use the brk() system call to move the "break" address,
- which marks the end of the addresses that exist in the process.
-
- Check how much virtual space your system has. If it's not enough for the
- kind of allocation you are doing in your project, you need to add more swap
- space and/or buy more RAM.
-
-
- >3) We are comming to the point: I must succesive realloc storage for a
- > certain variable v. And if realloc fails, i want have the recent correct
- > memory allocation for v containing the right value (look at 'temp' in the
- > code). I need a save solution!
-
- Then write your own realloc() routine or macro which allocates new storage, and
- if successful, copies the data into it and gives you the new pointer, and then
- frees the original copy. Give it different semantics, something like:
-
- int my_realloc(void **old2new, size_t size);
-
- If my_realloc() fails, it can be set up so that it will _not_ change the value
- of the void *old2new and returns some error code, otherwise it will update the
- value of the pointer.
-
-
- I wish I could tell you that unsuccessful realloc() does no harm to the
- original allocated block, but I can't say that this is guaranteed on every
- system.
-
- The manual page on the HP-UX system does make such a guarantee:
-
- RETURN VALUE
- Upon successful completion, malloc(), realloc(), and calloc() return a
- pointer to space suitably aligned (after possible pointer coercion)
- for storage of any type of object. Otherwise, they return a NULL
- pointer. If realloc() returns a NULL pointer, the memory pointed to
- by the original pointer is left intact.
-
- It probably works the same way on other UNIX systems. So as long as you
- remember what the old pointer was, you should be able to keep using it if
- realloc() failed.
- --
-
-